import os
import csv
import time
import random
import pandas as pd
from selenium import webdriver

def uni_name():
    folder_path = 'D:\桌面\考研大数据\数据分析\dataWash'  # 需要读取的文件夹
    csv_files = [os.path.join(folder_path, f) for f in os.listdir(
        folder_path) if f.endswith('.csv')]  # 提取文件夹里的所有csv文件
    university_names = []  # 存大学名
    for file in csv_files:
        data = pd.read_csv(file, header=0)  # 开始读取csv文件
        for university_name in data['招生单位']:
            university_names.append(university_name.split(')')[-1])
    # print(university_names)
    # print(len(university_names))
    with open('D:\桌面\考研大数据\地图可视化\data\大学名.csv', 'a', newline='') as f:
        csv_write = csv.writer(f)
        csv_write.writerow(['学校名'])
        for name in university_names:
            csv_write.writerow([name])
    uni_name_wash()

# 去重和去除空值
def  uni_name_wash():
    data = pd.read_csv('D:\桌面\考研大数据\地图可视化\data\大学名.csv',
                       encoding='GBK', header=0)
    data.drop_duplicates(subset=['学校名'], keep='first', inplace=True)
    data.dropna(axis=0, how='any', inplace=True)  # 去除空值
    # print(data['学校名'])
    # print(len(data['学校名']))
    with open('D:\桌面\考研大数据\地图可视化\data\大学名(wash).csv', 'a', newline='') as f:
        csv_write = csv.writer(f)
        csv_write.writerow(['学校名'])
        for name in data['学校名']:
            csv_write.writerow([name])

#获取学校经纬度
def university_info_collect():
    data = pd.read_csv('D:\桌面\考研大数据\地图可视化\data\大学名(wash).csv',
                       encoding='GBK', header=0)
    data_new = pd.read_csv('D:\桌面\考研大数据\地图可视化\data\大学（维度_经度）.csv',encoding='GBK', header=0)
    # print(len(data['学校名']))
    driver.get("https://lbs.amap.com/tools/picker")

    #运行过程中验证时注释掉
    with open('D:\桌面\考研大数据\地图可视化\data\大学（维度_经度）.csv', 'a', newline='') as f:
        csv_write = csv.writer(f)
        csv_write.writerow(['大学名', '维度', '经度'])

    for search_name in data['学校名'][len(data_new['大学名']):]:
        # driver.get("https://lbs.amap.com/tools/picker")
        if(search_name != ''):
            driver.find_element_by_xpath(
                '//*[@id="txtSearch"]').send_keys(search_name)
            time.sleep(random.randint(5, 20))
            driver.find_element_by_xpath(
                '//*[@id="myPageTop"]/table/tbody/tr[2]/td[1]/a').click()
            time.sleep(random.randint(5, 20))
            info = driver.find_element_by_xpath('//*[@id="txtCoordinate"]')
            university_info = info.get_attribute('value')
            time.sleep(random.randint(5, 20))
            driver.find_element_by_xpath(
                '//*[@id="txtSearch"]').clear()
            latitude = university_info.split(',')[1]
            longitude = university_info.split(',')[0]
            with open('D:\桌面\考研大数据\地图可视化\data\大学（维度_经度）.csv', 'a', newline='') as f:
                csv_write = csv.writer(f)
                csv_write.writerow([search_name, latitude, longitude])


if __name__ == '__main__':
    latitude = []  # 维度
    longitude = []  # 经度
    # uni_name()        #获取学校名，去重和去空值

    # option = webdriver.ChromeOptions()
    # option.add_argument('headless')
    # driver = webdriver.Chrome(options=option)  # 浏览器驱动,不打开浏览器运行
    driver = webdriver.Chrome()  # 打开浏览器

    university_info_collect()  # 获取学校经纬度信息
